//
//  DownLoadViewController.m
//  A_Comic
//
//  Created by sos on 16/1/25.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "DownLoadViewController.h"
#import "CatalogueCollectionCell.h"
#import "HomePageModel.h"
#import <AFNetworking.h>
#import "MyDownLoadViewController.h"
@interface DownLoadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain) UICollectionView *collection;
@property (nonatomic,retain) NSMutableArray *dataArrOne;
@property (nonatomic,assign) NSInteger q;
//@property (nonatomic,assign) BOOL isB;
@property (nonatomic,retain) NSMutableArray *dataArrTwo;//保留下载数据
@property (nonatomic,retain) NSMutableArray *dataArrThree;
@end

@implementation DownLoadViewController

- (void)dealloc
{
    [_dataArrTwo release];
    [_m release];
    [_dataArrOne release];
    [_collection release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    _isB = YES;
    self.navigationController.navigationBarHidden = NO;
    self.dataArrThree = [NSMutableArray array];
    self.dataArrTwo = [NSMutableArray array];
    // Do any additional setup after loading the view.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 20;
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    layout.itemSize = CGSizeMake((self.view.frame.size.width-80)/3, 30);
    
    
    
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50) collectionViewLayout:layout];
    
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    self.collection.backgroundColor = [UIColor whiteColor];
    [self.collection registerClass:[CatalogueCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.collection];
    [layout release];
    [self.collection release];
    
    //全选按钮
    UIButton *allButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    allButton.backgroundColor = [UIColor redColor];
    [allButton setTitle:@"全选" forState:UIControlStateNormal];
    [allButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    allButton.frame = CGRectMake(0, self.collection.frame.size.height, self.view.frame.size.width/2-11, 50);
    [self.view addSubview:allButton];
    
    //中间的线
    UIView *xianView = [[UIView alloc]initWithFrame:CGRectMake(allButton.frame.size.width+10, allButton.frame.origin.y, 1, allButton.frame.size.height)];
    xianView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:xianView];
    
    
    //下载所选
    UIButton *downAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    downAllButton.backgroundColor = [UIColor blueColor];
    [downAllButton setTitle:@"下载所选" forState:UIControlStateNormal];
    [downAllButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    downAllButton.frame = CGRectMake(xianView.frame.origin.x+10, allButton.frame.origin.y, allButton.frame.size.width+1, allButton.frame.size.height);
    [downAllButton addTarget:self action:@selector(download:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:downAllButton];
    
    [self data];
}

-(void)data
{
    NSString *str = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Comic/comicChapterListForIosV2/uin/0/local_version/3.5.1/channel/1001/guest_id/FBB11919-34E9-4168-9491-811B4C0BDBD6/comic_id/%@",self.m.comic_id];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"];
        
        self.dataArrOne = [HomePageModel modelHandlerWithArray:arr];
        
        for (NSInteger i = 0; i < self.dataArrOne.count/2; i++) {
            
            
            [self.dataArrOne exchangeObjectAtIndex:i withObjectAtIndex:self.dataArrOne.count-1-i];
            
        }

        [self.collection reloadData];
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];

    
}

//协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArrOne.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CatalogueCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HomePageModel *m = self.dataArrOne[indexPath.item];
    cell.catalogueLabel.text = m.btn_str;



        cell.backgroundColor = [UIColor grayColor];

    cell.tag = 5555+indexPath.item;

    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CatalogueCollectionCell *cell = [self.view viewWithTag:5555+indexPath.item];
    if (cell.backgroundColor == [UIColor grayColor]) {
        
        cell.backgroundColor = [UIColor greenColor];
        HomePageModel *m = self.dataArrOne[indexPath.item];
        NSLog(@"~~%@",m.btn_str);
        m.cover_url = self.m.cover_url;
        [self.dataArrTwo addObject:m];
//        [self.dataArrTwo addObject:self.m.cover_url];
        NSLog(@"!!!%@",self.dataArrTwo);
        
    }else{
        cell.backgroundColor = [UIColor grayColor];
        [self.dataArrTwo removeObjectAtIndex:indexPath.item];
    }
    

    
}

//下载方法
- (void)download:(UIButton *)btn
{
    
//    static MyDownLoadViewController *myDownLoad;
   MyDownLoadViewController *myDownLoad = [[MyDownLoadViewController alloc]init];
    for (HomePageModel *m in self.dataArrTwo) {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager GET:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Comic/chapterPictureListForIos/uin/0/local_version/3.5.1/channel/1001/guest_id/FBB11919-34E9-4168-9491-811B4C0BDBD6/comic_id/%@/cid/%@/uin/0/local_version/3.5.1/channel/1001/guest_id/FBB11919-34E9-4168-9491-811B4C0BDBD6",m.comic_id,m.cid] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSArray *arr = responseObject[@"data"][@"img_infos"];
            for (NSDictionary *dic in arr) {
                HomePageModel *m = [[HomePageModel alloc]init];
                [m setValuesForKeysWithDictionary:dic];
                [self.dataArrThree addObject:m];
                [self.dataArrThree addObject:m.current_img_url];
                [m release];
            }
            
            myDownLoad.arr = self.dataArrThree;
            [self.collection reloadData];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            
        }];

        
    }
   
    [self.navigationController pushViewController:myDownLoad animated:YES];
    
    NSLog(@"dianguole");
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
